package com.hilaver.dzmis.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiMeasureSpecification;
import com.hilaver.dzmis.basicinfo.BiMeasureSpecificationItem;
import com.hilaver.dzmis.codetable.CtArmor1Type;
import com.hilaver.dzmis.codetable.CtArmorSet;
import com.hilaver.dzmis.codetable.CtArmorType;
import com.hilaver.dzmis.codetable.CtCareLabelMaterial;
import com.hilaver.dzmis.codetable.CtCollarType;
import com.hilaver.dzmis.codetable.CtDesignType;
import com.hilaver.dzmis.codetable.CtMaterialType;
import com.hilaver.dzmis.codetable.CtMsiDescription;
import com.hilaver.dzmis.codetable.CtProductIdentificationStyle;
import com.hilaver.dzmis.codetable.CtSeasonNumber;
import com.hilaver.dzmis.codetable.CtVersion;
import com.hilaver.dzmis.codetable.CtYarnType;
import com.hilaver.dzmis.dao.ProductIdentificationDAO;
import com.hilaver.dzmis.order.OrderProduct;
import com.hilaver.dzmis.product.ProductAccessoryButton;
import com.hilaver.dzmis.product.ProductAccessoryHang;
import com.hilaver.dzmis.product.ProductAccessoryOthers;
import com.hilaver.dzmis.product.ProductAccessoryZip;
import com.hilaver.dzmis.product.ProductDZColor;
import com.hilaver.dzmis.product.ProductIdentification;
import com.hilaver.dzmis.product.ProductLaunching;
import com.hilaver.dzmis.product.ProductMeasureSpecificationItem;
import com.hilaver.dzmis.product.ProductOrderItem;
import com.hilaver.dzmis.product.ProductOrderItemColor;
import com.hilaver.dzmis.product.ProductPrice;
import com.hilaver.dzmis.product.ProductWorkFlow;
import com.hilaver.dzmis.product.ProductYarn;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class ProductIdentificationServiceImpl extends AbstractBaseService {

	public final static String COLUMN_SEASON_NUMBER = "ctSeasonNumber";
	
	public final static String COLUMN_VERSION = "ctVersion";
	
	public final static String COLUMN_MACHINE_TYPE = "ctMachineType";

	public final static String COLUMN_TYPE = "ctCostumeType";

	public final static String COLUMN_ORDER_NUMBER = "orderNumber";

	public final static String COLUMN_YARN_NUMBER = "yarnNumber";
	
	public final static String COLUMN_YARN_NAME = "yarnName";

	public final static String COLUMN_IS_SIMILARY = "isSimilary";

	public final static String COLUMN_IS_NEW_FASHIONED = "isNewfashioned";
	
	public final static String COLUMN_CT_COLLAR_TYPE = "ctCollarType";

	public final static String COLUMN_IS_DIFFICULTY = "isDifficulty";

	public final static String COLUMN_DHL_NUMBER = "dhlNumber";

	public final static String COLUMN_DESIGN_TYPE = "ctDesignType";

	public final static String COLUMN_STYLE = "style";

	public final static String COLUMN_CUSTOMER_LINKMAN = "customerLinkman";
	
	public final static String COLUMN_MODEL = "model";

	public final static String COLUMN_REFERENCE = "reference";
	
	public final static String COLUMN_REFERENCE_ID = "biReferenceId";

	private final static String COLUMN_CREATE_DATE = "createDate";

	public final static String COLUMN_CHART_VERSION = "chartVersion";

	public final static String COLUMN_PATRONAGE_NUMBER = "patronageNumer";

	public final static String COLUMN_FRONT = "front";

	public final static String COLUMN_BACK = "back";

	public final static String COLUMN_SLEEVES = "sleeves";

	public final static String COLUMN_BOTTON_THRUM = "bottomThrum";

	public final static String COLUMN_SLEEVES_THRUM = "sleevesThrum";

	public final static String COLUMN_NECKLINE_THRUM = "necklineThrum";
	
	private final static String COLUMN_NEEDLE =  "needle";
	
	private final static String COLUMN_WEIGHT = "weight";

	public final static String COLUMN_COMMENT = "comment";
	
	public final static String COLUMN_UNIT_PRICE = "unitPrice";
	
	public final static String COLUMN_TAG_COMMENT = "tagComment";

	private final static String COLUMN_CHART_REPOSITORY = "biChartRepositoryId";

	public final static String COLUMN_PHOTO_NAME = "photoName";

	public final static String COLUMN_CHART_NAME = "chartName";
	
	public final static String COLUMN_FRONT_PHOTO_NAME = "frontPhotoName";

	public final static String COLUMN_BACK_PHOTO_NAME = "backPhotoName";
	
	public final static String COLUMN_OTHER_PHOTO_NAME = "otherPhotoName";
	
	public final static String COLUMN_MEASURE_SPECIFICATION_ID =  "biMeasureSpecificationId";
	
	public final static String COLUMN_MEASURE_SPECIFICATION_NAME =  "biMeasureSpecificationName";
	
	public final static String COLUMN_CREATE_ID = "creatorId";

	public final static String COLUMN_MODIFIER_ID = "modiferId";

	public final static String COLUMN_OWNER_ID = "ownerId";

	public final static String COLUMN_MODIFY_DATE = "modifyDate";

	public final static String COLUMN_IS_HISTORY = "isHistory";
	
	public final static String COLUMN_PRODUCTIDENTIFICATION_ID = "productIdentification.id";
	
	public final static String COLUMN_FIL = "fil";
	
	public final static String COLUMN_PRECENTAGE = "precentage";
	
	public final static String COLUMN_YARN = "biYarnId";
	
	public final static String COLUMN_POI_PI_ID = "productOrderItem.productIdentification.id";
	
	public final static String COLUMN_ITEM_NUMBER = "number";
	
	private CtServiceImpl ctService;
	
	private ProductWorkFlowServiceImpl pwfServiceImpl;
	
	private ProductPriceServiceImpl ppsl;
	
	private ProductIdentificationDAO dao;
	
	public ProductIdentificationServiceImpl() {
		super();
		this.ctService = new CtServiceImpl();
		this.dao = new ProductIdentificationDAO();
	}

	public String edit(ProductIdentification pi) throws Exception {
		this.pwfServiceImpl = new ProductWorkFlowServiceImpl();
		if(pi.getCtVersion().length() <= 1){
			String version = "0" + pi.getCtVersion();
			pi.setCtVersion(version);
		}
		this.ppsl = new ProductPriceServiceImpl();
		if (pi.getId() == null) {
			ProductWorkFlow pwf = this.pwfServiceImpl.getDefault();
			pwf.setProductIdentification(pi);
			pi.setProductWorkFlow(pwf);
			ProductPrice pp = new ProductPrice();
			pp.setProductIdentification(pi);
			pi.setProductPrice(pp);
			pi.setModifyDate(new Date());
			pi.setIsConfirmed("02");
			
			Set<ProductMeasureSpecificationItem> productMeasureSpecificationItems = null;
			if(pi.getBiMeasureSpecification() != null && pi.getSimilarId() == null){
				BiMeasureSpecification biMeasureSpecification = (BiMeasureSpecification)this.dao.get(BiMeasureSpecification.class.getName(), pi.getBiMeasureSpecification().getId()) ;
				productMeasureSpecificationItems = 
					cloneMeasureSpecificationItem(biMeasureSpecification.getBiMeasureSpecificationItems());
				
				pi.setProductMeasureSpecificationItems(productMeasureSpecificationItems);
			}
			
			if(pi.getIsSketch1Deleted()){
				pi.setSketch1(null);
				pi.setSketch1Name(null);
			}
			if(pi.getIsSketch2Deleted()){
				pi.setSketch2(null);
				pi.setSketch2Name(null);
			}
			if(pi.getIsSketch3Deleted()){
				pi.setSketch3(null);
				pi.setSketch3Name(null);
			}
			if(pi.getIsSketch4Deleted()){
				pi.setSketch4(null);
				pi.setSketch4Name(null);
			}
			if(pi.getIsInprocess1() == null){
				pi.setIsInprocess1("01");
			}
			if(pi.getIsInprocess2() == null){
				pi.setIsInprocess2("01");
			}
			this.dao.saveOrUpdate(pi);
			//this.dao.saveOrUpdate(pwf);
			//this.dao.saveOrUpdate(pp);
			
			if(productMeasureSpecificationItems != null && pi.getSimilarId() == null){
				for(ProductMeasureSpecificationItem pmsi : productMeasureSpecificationItems){
					pmsi.setProductIdentification(pi);
					this.dao.saveOrUpdate(pmsi);
				}
			}
			
			if(pi.getSimilarId() != null){
				this.cloneProductIdentification(pi);
			}
			
		} else {
			ProductIdentification old = (ProductIdentification)this.dao.get(ProductIdentification.class.getName(), pi.getId());
			//pi.setCreateDate(old.getCreateDate());
			if(pi.getSketch1() == null){
				pi.setSketch1(old.getSketch1());
				pi.setSketch1Name(old.getSketch1Name());
			}
			if(pi.getSketch2() == null){
				pi.setSketch2(old.getSketch2());
				pi.setSketch2Name(old.getSketch2Name());
			}
			if(pi.getSketch3() == null){
				pi.setSketch3(old.getSketch3());
				pi.setSketch3Name(old.getSketch3Name());
			}
			if(pi.getSketch4() == null){
				pi.setSketch4(old.getSketch4());
				pi.setSketch4Name(old.getSketch4Name());
			}
			if(pi.getIsSketch1Deleted()){
				pi.setSketch1(null);
				pi.setSketch1Name(null);
			}
			if(pi.getIsSketch2Deleted()){
				pi.setSketch2(null);
				pi.setSketch2Name(null);
			}
			if(pi.getIsSketch3Deleted()){
				pi.setSketch3(null);
				pi.setSketch3Name(null);
			}
			if(pi.getIsSketch4Deleted()){
				pi.setSketch4(null);
				pi.setSketch4Name(null);
			}
			
			pi.setIsDifficulty(old.getIsDifficulty());
			pi.setIsSimilary(old.getIsSimilary());
			pi.setIsNewfashioned(old.getIsNewfashioned());
			pi.setDhlNumber(old.getDhlNumber());
			pi.setCtStature(old.getCtStature());
			pi.setUnitPrice(old.getUnitPrice());
			pi.setTagComment(old.getTagComment());
			pi.setProcurement(old.getProcurement());
			pi.setProductPackage(old.getProductPackage());
			
			pi.setConfirmedDate(old.getConfirmedDate());
			pi.setYarnSize1(old.getYarnSize1());
			pi.setYarnSize2(old.getYarnSize2());
			pi.setYarnSize3(old.getYarnSize3());
			pi.setYarnSize4(old.getYarnSize4());
			pi.setYarnSize5(old.getYarnSize5());
			pi.setYarnSize6(old.getYarnSize6());
			pi.setYarnSize7(old.getYarnSize7());
			pi.setYarnSize8(old.getYarnSize8());
			pi.setCoefficient(old.getCoefficient());
			pi.setIsConfirmed(old.getIsConfirmed());
			pi.setIsInprocess1(old.getIsInprocess1());
			pi.setIsInprocess2(old.getIsInprocess2());
			
			pi.setBiMeasureSpecification(old.getBiMeasureSpecification());
			pi.setWeight(old.getWeight());
			
			if(old.getProductWorkFlow() == null){
				ProductWorkFlow pwf = this.pwfServiceImpl.getDefault();
				pwf.setProductIdentification(pi);
				pi.setProductWorkFlow(pwf);
				this.dao.saveOrUpdate(pwf);
			}
			if(old.getProductPrice() == null){
				ProductPrice pp = new ProductPrice();
				pp.setProductIdentification(pi);
				pi.setProductPrice(pp);
				this.dao.saveOrUpdate(pp);
			}
			
			this.dao.getSession().evict(old);
			
			pi.setModifyDate(new Date());

			this.dao.saveOrUpdate(pi);
		}
		
		String operation = null;
		if(pi.getId() != null){
			operation = "02";
		}else{
			operation = "01";
		}
		super.editSysMbox(pi, operation);
		if(pi.getBiMeasureSpecification() != null){
			return "{success: true, id: " + pi.getId() +", msId: " + pi.getBiMeasureSpecification().getId() + "}";
		}else{
			return "{success: true, id: " + pi.getId() + "}";
		}
	}
	
	public String editMSItem(Object obj) throws Exception{
		ProductMeasureSpecificationItem pmsi = (ProductMeasureSpecificationItem)obj;
		if(pmsi.getId() != null){
			ProductMeasureSpecificationItem old = (ProductMeasureSpecificationItem)this.dao.get(ProductMeasureSpecificationItem.class.getName(), pmsi.getId());
			BiMeasureSpecification bms = old.getProductIdentification().getBiMeasureSpecification();
			Set<BiMeasureSpecificationItem> bisiSet = bms.getBiMeasureSpecificationItems();
			for(BiMeasureSpecificationItem bmsi : bisiSet){
				if(bmsi.getNumber().equals(pmsi.getNumber())){
					if(bmsi.getSize1() != null && pmsi.getSize1() != null  && bmsi.getSize1().equals(pmsi.getSize1())) pmsi.setIsModified1("02");
					else pmsi.setIsModified1("01");
					
					if(bmsi.getSize2() != null && pmsi.getSize2() != null  && bmsi.getSize2().equals(pmsi.getSize2())) pmsi.setIsModified2("02");
					else pmsi.setIsModified2("01");
					
					if(bmsi.getSize3() != null && pmsi.getSize3() != null  && bmsi.getSize3().equals(pmsi.getSize3())) pmsi.setIsModified3("02");
					else pmsi.setIsModified3("01");
					
					if(bmsi.getSize4() != null && pmsi.getSize4() != null  && bmsi.getSize4().equals(pmsi.getSize4())) pmsi.setIsModified4("02");
					else pmsi.setIsModified4("01");
					
					if(bmsi.getSize5() != null && pmsi.getSize5() != null  && bmsi.getSize5().equals(pmsi.getSize5())) pmsi.setIsModified5("02");
					else pmsi.setIsModified5("01");
					
					if(bmsi.getSize6() != null && pmsi.getSize6() != null  && bmsi.getSize6().equals(pmsi.getSize6())) pmsi.setIsModified6("02");
					else pmsi.setIsModified6("01");
					
					if(bmsi.getSize7() != null && pmsi.getSize7() != null  && bmsi.getSize7().equals(pmsi.getSize7())) pmsi.setIsModified7("02");
					else pmsi.setIsModified7("01");
					
					if(bmsi.getSize8() != null && pmsi.getSize8() != null  && bmsi.getSize8().equals(pmsi.getSize8())) pmsi.setIsModified8("02");
					else pmsi.setIsModified8("01");
					
					continue;
				}
			}
			this.dao.getSession().evict(old);
		}
		return super.edit(pmsi);
	}

	@Override
	public String delete(int id) throws Exception {
		super.editSysMbox(ProductIdentification.class.getName(), id);
		return super.delete(ProductIdentification.class.getName(), id);
	}
	
	public String deleteMSItem(int id) throws Exception {
		return super.delete(ProductMeasureSpecificationItem.class.getName(), id);
	}
	
//	public String deleteYarn(int id) throws Exception {
//		ProductYarn py = (ProductYarn)this.dao.get(ProductYarn.class.getName(), id);
//		Float stock = py.getBiYarn().getStock();
//		stock += py.getQuantity();
//		py.getBiYarn().setStock(stock);
//		this.dao.saveOrUpdate(py.getBiYarn());
//		return super.delete(ProductYarn.class.getName(), id);
//	}
	
	@Override
	public String get(int id) throws Exception {
		ProductIdentification pi = (ProductIdentification)this.dao.get(
				ProductIdentification.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append("<" + COLUMN_ID + ">");
		xmlSB.append(pi.getId());
		xmlSB.append("</" + COLUMN_ID + ">");
		
		xmlSB.append("<" + COLUMN_CUSTOMER_ID + ">");
		xmlSB.append(pi.getBiFranceCustomer().getId());
		xmlSB.append("</" + COLUMN_CUSTOMER_ID + ">");
		
		if(pi.getBiMeasureSpecification() != null){
			xmlSB.append("<" + COLUMN_MEASURE_SPECIFICATION_ID + ">");
			xmlSB.append(pi.getBiMeasureSpecification().getId());
			xmlSB.append("</" + COLUMN_MEASURE_SPECIFICATION_ID + ">");
			xmlSB.append("<" + COLUMN_MEASURE_SPECIFICATION_NAME + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(pi.getBiMeasureSpecification().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_MEASURE_SPECIFICATION_NAME + ">");
		}
		
		xmlSB.append("<sketch1Name>");		
		xmlSB.append(Constants.XML_CDATA[0]);
		if (StringUtils.isEmpty(pi.getSketch1Name())) {
			xmlSB.append(NO_IMAGE_NAME);
		} else {
			writeFile(realFileDir + pi.getSketch1Name(), pi.getSketch1());
			xmlSB.append(pi.getSketch1Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</sketch1Name>");
		
		xmlSB.append("<sketch2Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if (StringUtils.isEmpty(pi.getSketch2Name())) {
			xmlSB.append(NO_IMAGE_NAME);
		} else {
			writeFile(realFileDir + pi.getSketch2Name(), pi.getSketch2());
			xmlSB.append(pi.getSketch2Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</sketch2Name>");

		xmlSB.append("<sketch3Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if (StringUtils.isEmpty(pi.getSketch3Name())) {
			xmlSB.append(NO_IMAGE_NAME);
		} else {
			writeFile(realFileDir + pi.getSketch3Name(), pi.getSketch3());
			xmlSB.append(pi.getSketch3Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</sketch3Name>");
		
		xmlSB.append("<sketch4Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if (StringUtils.isEmpty(pi.getSketch4Name())) {
			xmlSB.append(NO_IMAGE_NAME);
		} else {
			writeFile(realFileDir + pi.getSketch4Name(), pi.getSketch4());
			xmlSB.append(pi.getSketch4Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</sketch4Name>");
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(pi));
		
		xmlSB.append(this.toXMLFragment(pi));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getWithDescription(int id) throws Exception {
		ProductIdentification pi = (ProductIdentification)this.dao.get(
				ProductIdentification.class.getName(), id);
		
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		xmlSB.append("<" + COLUMN_ID + ">");
		xmlSB.append(pi.getId());
		xmlSB.append("</" + COLUMN_ID + ">");
		
		xmlSB.append(this.toXMLFragment(pi));
		
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getYarn(int id) throws Exception {
		ProductYarn py = (ProductYarn)this.dao.get(ProductYarn.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(py));
		
		xmlSB.append("<ctYarnType>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtYarnType.class.getName(), py.getBiYarn().getCtYarnType(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctYarnType>");
		
		xmlSB.append("<ctMaterialType>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtMaterialType.class.getName(), py.getBiYarn().getCtYarnMaterial1(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctMaterialType>");
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(py.getBiYarn()));
		xmlSB.append("<" + COLUMN_YARN +">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(py.getBiYarn().getId());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_YARN +">");
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {

		return null;
	}

	public String getAllPagination(int page, String sort, String order,
			String[] filter) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
						
		List<ProductIdentification> objList = this.dao.getAllPagination(ProductIdentification.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filter);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (ProductIdentification obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<" + COLUMN_ID + ">");
			xmlSB.append(obj.getId());
			xmlSB.append("</" + COLUMN_ID + ">");
			xmlSB.append(this.toXMLFragment((ProductIdentification) obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductIdentification.class.getName(),
				filter));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAllIsNotHistoryPagination(int page, String sort, String order,
			String[] filter) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
						
		List<ProductIdentification> objList = this.dao.getAllIsNotHistoryPagination(ProductIdentification.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filter);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (ProductIdentification obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<" + COLUMN_ID + ">");
			xmlSB.append(obj.getId());
			xmlSB.append("</" + COLUMN_ID + ">");
			xmlSB.append(this.toXMLFragment((ProductIdentification) obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCountIsNotHistory(ProductIdentification.class.getName(),
				filter));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAllSameAsPagination(String sort, String order,
			String[] filter) throws Exception {
		int offset = 0;
						
		List<ProductIdentification> objList = this.dao.getAllSaveAsPagination(sort, order, filter);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (ProductIdentification obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<" + COLUMN_ID + ">");
			xmlSB.append(obj.getId());
			xmlSB.append("</" + COLUMN_ID + ">");
			xmlSB.append(this.toXMLFragment((ProductIdentification) obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(1);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(objList.size());
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String search(int page, String sort, String order,
			String[] filter) throws Exception{
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<ProductIdentification> objList = this.dao.getAllPagination(ProductIdentification.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filter);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (ProductIdentification obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<" + COLUMN_ID + ">");
			xmlSB.append(obj.getId());
			xmlSB.append("</" + COLUMN_ID + ">");
			
			xmlSB.append(this.toXMLFragment((ProductIdentification) obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductIdentification.class.getName(),
				filter));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAllFilterPagination(int page, String sort, String order,
			String[] filter) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		
		List<ProductIdentification> objList = this.dao.getAllPagination(ProductIdentification.class
				.getName(), offset, Constants.PAGE_SIZE, sort, order, filter);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (ProductIdentification obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<" + COLUMN_ID + ">");
			xmlSB.append(obj.getId());
			xmlSB.append("</" + COLUMN_ID + ">");
			xmlSB.append(this.toXMLFragment((ProductIdentification) obj));

			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}

		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductIdentification.class.getName(),
				filter));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAllInProcessPagination(int page, String sort, String order,
			String[] filters) throws Exception {
		int offset = Constants.PAGE_SIZE * (page - 1);
		List<ProductIdentification> objList = this.dao.getAllInProcessPagination(offset, Constants.PAGE_SIZE, sort, order, filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (ProductIdentification obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<" + COLUMN_ID + ">");
			xmlSB.append(obj.getId());
			xmlSB.append("</" + COLUMN_ID + ">");
			
			if(obj.getOrderProducts().size() > 0){
				Iterator it = obj.getOrderProducts().iterator();
				xmlSB.append("<orderId>");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(((OrderProduct)it.next()).getId());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</orderId>");
			}
			
			xmlSB.append(this.toXMLFragment((ProductIdentification) obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getAllInProcessCount(filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	

	public String toXMLFragment(ProductIdentification pi) throws Exception {
		StringBuffer xmlSB = new StringBuffer();
		/*
		xmlSB.append("<" + COLUMN_ID + ">");
		xmlSB.append(pi.getId());
		xmlSB.append("</" + COLUMN_ID + ">");
		
		xmlSB.append("<" + COLUMN_PRODUCT_FAMILY + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtProductFamily.class.getName(), pi.getCtProductFamily(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_PRODUCT_FAMILY + ">");
		
		xmlSB.append("<" + COLUMN_PRODUCT_FAMILY + "Code>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getCtProductFamily());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_PRODUCT_FAMILY + "Code>");
		*/
		xmlSB.append("<" + COLUMN_MACHINE_TYPE + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		
		
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_MACHINE_TYPE + ">");
		
		xmlSB.append("<" + COLUMN_CT_COLLAR_TYPE + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtCollarType.class.getName(),pi.getCtCollarType(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_CT_COLLAR_TYPE + ">");
		
		xmlSB.append("<ctProductIdentificationStyle>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtProductIdentificationStyle.class.getName(),pi.getCtProductIdentificationStyle(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctProductIdentificationStyle>");
		
		xmlSB.append("<" + COLUMN_IS_SIMILARY + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getIsSimilary()==null?"":pi.getIsSimilary());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_IS_SIMILARY + ">");
		
		xmlSB.append("<" + COLUMN_IS_NEW_FASHIONED + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getIsNewfashioned()==null?"":pi.getIsNewfashioned());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_IS_NEW_FASHIONED + ">");
		
		xmlSB.append("<" + COLUMN_IS_DIFFICULTY + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getIsDifficulty()==null?"":pi.getIsDifficulty());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_IS_DIFFICULTY + ">");
		
		xmlSB.append("<" + COLUMN_DESIGN_TYPE + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtDesignType.class.getName(),pi.getCtDesignType(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_DESIGN_TYPE + ">");
		
		xmlSB.append("<" + COLUMN_SEASON_NUMBER + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(this.ctService.getDescription(CtSeasonNumber.class.getName(), pi.getCtSeasonNumber(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_SEASON_NUMBER + ">");
		
		xmlSB.append("<ctSeasonNumberCode>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getCtSeasonNumber());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</ctSeasonNumberCode>");
		
		xmlSB.append("<" + COLUMN_CREATE_DATE + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(StringUtils.toString(pi.getCreateDate()));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_CREATE_DATE + ">");
		
		xmlSB.append("<" + COLUMN_VERSION + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		//System.out.println(pi.getId() + " " + pi.getCtVersion());
		xmlSB.append(this.ctService.getDescription(CtVersion.class.getName(), pi.getCtVersion(), locale));
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_VERSION + ">");		
		
		if(pi.getFront() != null && pi.getFront().getId() != null){
			xmlSB.append("<frontId>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(pi.getFront().getId());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</frontId>");
			
			//ctArmorType + ' ' + ctArmor1Type + ' ' + armorTypeE + ' ' + ctArmorSet
			xmlSB.append("<frontReference>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtArmorType.class.getName(), pi.getFront().getCtArmorType(), locale));
			xmlSB.append(" ");
			xmlSB.append(this.ctService.getDescription(CtArmor1Type.class.getName(), pi.getFront().getCtArmor1Type(), locale));
			xmlSB.append(" ");
			xmlSB.append(pi.getFront().getArmorTypeE()==null?"":pi.getFront().getArmorTypeE());
			xmlSB.append(" ");
			xmlSB.append(this.ctService.getDescription(CtArmorSet.class.getName(), pi.getFront().getCtArmorSet(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</frontReference>");
			
			xmlSB.append("<frontPhotoName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if (StringUtils.isEmpty(pi.getFront().getPhoto1Name())) {
				xmlSB.append(NO_IMAGE_NAME);
			} else {
				writeFile(realFileDir + pi.getFront().getPhoto1Name(), pi.getFront().getPhoto1());
				xmlSB.append(pi.getFront().getPhoto1Name());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</frontPhotoName>");
		}else{
			xmlSB.append("<frontPhotoName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(NO_IMAGE_NAME);
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</frontPhotoName>");
		}
		
		if(pi.getBack() != null && pi.getBack().getId() != null){
			xmlSB.append("<backId>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(pi.getBack().getId());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</backId>");
			
			xmlSB.append("<backReference>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtArmorType.class.getName(), pi.getBack().getCtArmorType(), locale));
			xmlSB.append(" ");
			xmlSB.append(this.ctService.getDescription(CtArmor1Type.class.getName(), pi.getBack().getCtArmor1Type(), locale));
			xmlSB.append(" ");
			xmlSB.append(pi.getBack().getArmorTypeE()==null?"":pi.getBack().getArmorTypeE());
			xmlSB.append(" ");
			xmlSB.append(this.ctService.getDescription(CtArmorSet.class.getName(), pi.getBack().getCtArmorSet(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</backReference>");
			
			xmlSB.append("<backPhotoName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if (StringUtils.isEmpty(pi.getBack().getPhoto1Name())) {
				xmlSB.append(NO_IMAGE_NAME);
			} else {
				writeFile(realFileDir + pi.getBack().getPhoto1Name(), pi.getBack().getPhoto1());
				xmlSB.append(pi.getBack().getPhoto1Name());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</backPhotoName>");
		}else{
			xmlSB.append("<backPhotoName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(pi.getBack().getPhoto1Name());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</backPhotoName>");
		}
		
		if(pi.getSleeves() != null && pi.getSleeves().getId() != null){
			xmlSB.append("<sleevesId>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(pi.getSleeves().getId());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</sleevesId>");
			
			xmlSB.append("<sleevesReference>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtArmorType.class.getName(), pi.getSleeves().getCtArmorType(), locale));
			xmlSB.append(" ");
			xmlSB.append(this.ctService.getDescription(CtArmor1Type.class.getName(), pi.getSleeves().getCtArmor1Type(), locale));
			xmlSB.append(" ");
			xmlSB.append(pi.getSleeves().getArmorTypeE()==null?"":pi.getSleeves().getArmorTypeE());
			xmlSB.append(" ");
			xmlSB.append(this.ctService.getDescription(CtArmorSet.class.getName(), pi.getSleeves().getCtArmorSet(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</sleevesReference>");
			
			xmlSB.append("<sleevesPhotoName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if (StringUtils.isEmpty(pi.getSleeves().getPhoto1Name())) {
				xmlSB.append(NO_IMAGE_NAME);
			} else {
				writeFile(realFileDir + pi.getSleeves().getPhoto1Name(), pi.getSleeves().getPhoto1());
				xmlSB.append(pi.getSleeves().getPhoto1Name());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</sleevesPhotoName>");
		}else{
			xmlSB.append("<sleevesPhotoName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(NO_IMAGE_NAME);
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</sleevesPhotoName>");
		}
		
		if(pi.getNecklineThrum() != null && pi.getNecklineThrum() != null){
			xmlSB.append("<necklineThrumId>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(pi.getNecklineThrum().getId());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</necklineThrumId>");
			
			xmlSB.append("<necklineThrumReference>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtArmorType.class.getName(), pi.getNecklineThrum().getCtArmorType(), locale));
			xmlSB.append(" ");
			xmlSB.append(this.ctService.getDescription(CtArmor1Type.class.getName(), pi.getNecklineThrum().getCtArmor1Type(), locale));
			xmlSB.append(" ");
			xmlSB.append(pi.getNecklineThrum().getArmorTypeE()==null?"":pi.getNecklineThrum().getArmorTypeE());
			xmlSB.append(" ");
			xmlSB.append(this.ctService.getDescription(CtArmorSet.class.getName(), pi.getNecklineThrum().getCtArmorSet(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</necklineThrumReference>");
			
			xmlSB.append("<necklineThrumPhotoName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if (StringUtils.isEmpty(pi.getNecklineThrum().getPhoto1Name())) {
				xmlSB.append(NO_IMAGE_NAME);
			} else {
				writeFile(realFileDir + pi.getNecklineThrum().getPhoto1Name(), pi.getNecklineThrum().getPhoto1());
				xmlSB.append(pi.getNecklineThrum().getPhoto1Name());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</necklineThrumPhotoName>");
		}else{
			xmlSB.append("<necklineThrumPhotoName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(NO_IMAGE_NAME);
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</necklineThrumPhotoName>");
		}
		
		if(pi.getBottomThrum() != null && pi.getBottomThrum().getId() != null){
			xmlSB.append("<bottomThrumId>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(pi.getBottomThrum().getId());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</bottomThrumId>");
			
			xmlSB.append("<bottomThrumReference>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtArmorType.class.getName(), pi.getBottomThrum().getCtArmorType(), locale));
			xmlSB.append(" ");
			xmlSB.append(this.ctService.getDescription(CtArmor1Type.class.getName(), pi.getBottomThrum().getCtArmor1Type(), locale));
			xmlSB.append(" ");
			xmlSB.append(pi.getBottomThrum().getArmorTypeE()==null?"":pi.getBottomThrum().getArmorTypeE());
			xmlSB.append(" ");
			xmlSB.append(this.ctService.getDescription(CtArmorSet.class.getName(), pi.getBottomThrum().getCtArmorSet(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</bottomThrumReference>");
			
			xmlSB.append("<bottomThrumPhotoName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if (StringUtils.isEmpty(pi.getBottomThrum().getPhoto1Name())) {
				xmlSB.append(NO_IMAGE_NAME);
			} else {
				writeFile(realFileDir + pi.getBottomThrum().getPhoto1Name(), pi.getBottomThrum().getPhoto1());
				xmlSB.append(pi.getBottomThrum().getPhoto1Name());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</bottomThrumPhotoName>");
		}else{
			xmlSB.append("<bottomThrumPhotoName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(NO_IMAGE_NAME);
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</bottomThrumPhotoName>");
		}
		
		if(pi.getSleevesThrum() != null && pi.getSleevesThrum().getId() != null){
			xmlSB.append("<sleevesThrumId>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(pi.getSleevesThrum().getId());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</sleevesThrumId>");
			
			xmlSB.append("<sleevesThrumReference>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtArmorType.class.getName(), pi.getSleevesThrum().getCtArmorType(), locale));
			xmlSB.append(" ");
			xmlSB.append(this.ctService.getDescription(CtArmor1Type.class.getName(), pi.getSleevesThrum().getCtArmor1Type(), locale));
			xmlSB.append(" ");
			xmlSB.append(pi.getSleevesThrum().getArmorTypeE()==null?"":pi.getSleevesThrum().getArmorTypeE());
			xmlSB.append(" ");
			xmlSB.append(this.ctService.getDescription(CtArmorSet.class.getName(), pi.getSleevesThrum().getCtArmorSet(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</sleevesThrumReference>");
			
			xmlSB.append("<sleevesThrumPhotoName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			if (StringUtils.isEmpty(pi.getSleevesThrum().getPhoto1Name())) {
				xmlSB.append(NO_IMAGE_NAME);
			} else {
				writeFile(realFileDir + pi.getSleevesThrum().getPhoto1Name(), pi.getSleevesThrum().getPhoto1());
				xmlSB.append(pi.getSleevesThrum().getPhoto1Name());
			}
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</sleevesThrumPhotoName>");
		}else{
			xmlSB.append("<sleevesThrumPhotoName>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(NO_IMAGE_NAME);
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</sleevesThrumPhotoName>");
		}
		
		
		
		xmlSB.append("<sketch1Name>");		
		xmlSB.append(Constants.XML_CDATA[0]);
		if (StringUtils.isEmpty(pi.getSketch1Name())) {
			xmlSB.append(NO_IMAGE_NAME);
		} else {
			writeFile(realFileDir + pi.getSketch1Name(), pi.getSketch1());
			xmlSB.append(pi.getSketch1Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</sketch1Name>");
		
		xmlSB.append("<sketch2Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if (StringUtils.isEmpty(pi.getSketch2Name())) {
			xmlSB.append(NO_IMAGE_NAME);
		} else {
			writeFile(realFileDir + pi.getSketch2Name(), pi.getSketch2());
			xmlSB.append(pi.getSketch2Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</sketch2Name>");

		xmlSB.append("<sketch3Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if (StringUtils.isEmpty(pi.getSketch3Name())) {
			xmlSB.append(NO_IMAGE_NAME);
		} else {
			writeFile(realFileDir + pi.getSketch3Name(), pi.getSketch3());
			xmlSB.append(pi.getSketch3Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</sketch3Name>");
		
		xmlSB.append("<sketch4Name>");
		xmlSB.append(Constants.XML_CDATA[0]);
		if (StringUtils.isEmpty(pi.getSketch4Name())) {
			xmlSB.append(NO_IMAGE_NAME);
		} else {
			writeFile(realFileDir + pi.getSketch4Name(), pi.getSketch4());
			xmlSB.append(pi.getSketch4Name());
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</sketch4Name>");
		
		xmlSB.append("<customerId>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getBiFranceCustomer().getId());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</customerId>");
		xmlSB.append("<" + COLUMN_CUSTOMER_NAME + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getBiFranceCustomer().getName());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_CUSTOMER_NAME + ">");
		xmlSB.append("<" + COLUMN_CUSTOEMR_DEPARTMENT + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getBiFranceCustomer().getDepartment()==null?"":pi.getBiFranceCustomer().getDepartment());
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_CUSTOEMR_DEPARTMENT + ">");
		
		if(pi.getBiFactory() != null){
			xmlSB.append("<factoryId>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(pi.getBiFactory().getId());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryId>");
			
			xmlSB.append("<factoryNameCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(pi.getBiFactory().getNameCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameCN>");
			
			xmlSB.append("<factoryNameEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(pi.getBiFactory().getNameEN()==null?"":pi.getBiFactory().getNameEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</factoryNameEN>");
			
			xmlSB.append("<addressFactoryCN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(pi.getBiFactory().getAddressFactoryCN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryCN>");
			
			xmlSB.append("<addressFactoryEN>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(pi.getBiFactory().getAddressFactoryEN());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</addressFactoryEN>");
		}
		
		if(pi.getBiMeasureSpecification() != null){
			xmlSB.append("<" + COLUMN_MEASURE_SPECIFICATION_ID + ">");
			xmlSB.append(pi.getBiMeasureSpecification().getId());
			xmlSB.append("</" + COLUMN_MEASURE_SPECIFICATION_ID + ">");
			xmlSB.append("<ctStatureCode>");
			xmlSB.append(pi.getBiMeasureSpecification().getCtStature());
			xmlSB.append("</ctStatureCode>");
		}
		
		xmlSB.append("<isSwatch>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getProductLaunchings().size() != 0 ? 1:0);
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</isSwatch>");
		
		xmlSB.append("<isYarned>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getProductDZColors().size() != 0?1:0);
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</isYarned>");
		
		xmlSB.append("<isOrdered>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getOrderProducts().size() != 0?1:0);
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</isOrdered>");
		
		xmlSB.append("<isButtoned>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getProductAccessoryButtons().size() != 0?1:0);
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</isButtoned>");
		
		xmlSB.append("<isZipped>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getProductAccessoryZips().size() != 0?1:0);
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</isZipped>");
		
		xmlSB.append("<isMeasurement>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getProductMeasureSpecificationItems().size() != 0?1:0);
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</isMeasurement>");
		
		xmlSB.append("<isAo>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getProductAccessoryOtherses().size() != 0?1:0);
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</isAo>");
		
		xmlSB.append("<isTrimmings>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append((pi.getProductAccessoryButtons().size() != 0 || pi.getProductAccessoryZips().size() != 0 ||  pi.getProductAccessoryOtherses().size() != 0)?1:0);
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</isTrimmings>");
		
		xmlSB.append("<isAddtoProcurement>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getProcurement() != null?1:0);
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</isAddtoProcurement>");
		
		xmlSB.append("<isAddtoPackage>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(pi.getProductPackage() != null?1:0);
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</isAddtoPackage>");
		
		if(pi.getBiMeasureSpecification() != null){
			xmlSB.append("<" + COLUMN_MEASURE_SPECIFICATION_ID + ">");
			xmlSB.append(pi.getBiMeasureSpecification().getId());
			xmlSB.append("</" + COLUMN_MEASURE_SPECIFICATION_ID + ">");
			xmlSB.append("<" + COLUMN_MEASURE_SPECIFICATION_NAME + ">");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(pi.getBiMeasureSpecification().getName());
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</" + COLUMN_MEASURE_SPECIFICATION_NAME + ">");
		}
		
		String mainComposition = "";
		if(pi.getProductAccessoryOtherses().size() > 0){
			for (Iterator iterator = pi.getProductAccessoryOtherses().iterator(); iterator.hasNext();) {
				ProductAccessoryOthers pAO = (ProductAccessoryOthers) iterator.next();
				if("15".equals(pAO.getBiAccessoryOthers().getCtAccessoryType())){//洗水麦，洗水麦的成分作为技术图的主要成分
					mainComposition += pAO.getBiAccessoryOthers().getPercentage1() + "% ";
					mainComposition += this.ctService.getDescription(CtCareLabelMaterial.class.getName(), pAO.getBiAccessoryOthers().getCtCareLabelMaterial1(), this.locale);
					mainComposition += "　";
					if(!StringUtils.isEmpty(pAO.getBiAccessoryOthers().getCtCareLabelMaterial2())){
						mainComposition += pAO.getBiAccessoryOthers().getPercentage2() + "% ";
						mainComposition += this.ctService.getDescription(CtCareLabelMaterial.class.getName(), pAO.getBiAccessoryOthers().getCtCareLabelMaterial2(), this.locale);
						mainComposition += "　";
					}
					
					if(!StringUtils.isEmpty(pAO.getBiAccessoryOthers().getCtCareLabelMaterial3())){
						mainComposition += pAO.getBiAccessoryOthers().getPercentage3() + "% ";
						mainComposition += this.ctService.getDescription(CtCareLabelMaterial.class.getName(), pAO.getBiAccessoryOthers().getCtCareLabelMaterial3(), this.locale);
						mainComposition += "　";
					}
					
					if(!StringUtils.isEmpty(pAO.getBiAccessoryOthers().getCtCareLabelMaterial4())){
						mainComposition += pAO.getBiAccessoryOthers().getPercentage4() + "% ";
						mainComposition += this.ctService.getDescription(CtCareLabelMaterial.class.getName(), pAO.getBiAccessoryOthers().getCtCareLabelMaterial4(), this.locale);
						mainComposition += "　";
					}
					
					if(!StringUtils.isEmpty(pAO.getBiAccessoryOthers().getCtCareLabelMaterial5())){
						mainComposition += pAO.getBiAccessoryOthers().getPercentage5() + "% ";
						mainComposition += this.ctService.getDescription(CtCareLabelMaterial.class.getName(), pAO.getBiAccessoryOthers().getCtCareLabelMaterial5(), this.locale);
						mainComposition += "　";
					}
				}
				
			}
		}
		xmlSB.append("<mainComposition>");
		xmlSB.append(Constants.XML_CDATA[0]);
		xmlSB.append(mainComposition);
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</mainComposition>");
		
		xmlSB.append("<" + COLUMN_ORDER_NUMBER + ">");
		xmlSB.append(Constants.XML_CDATA[0]);
		if(pi.getOrderProducts().size() > 0){
			Iterator it = pi.getOrderProducts().iterator();
			xmlSB.append(((OrderProduct)it.next()).getOrderNumber());
		}else if(pi.getProductLaunchings().size() > 0){
			Iterator it = pi.getProductLaunchings().iterator();
			xmlSB.append(((ProductLaunching)it.next()).getOrderNumber());
		}else{
			xmlSB.append("");
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append("</" + COLUMN_ORDER_NUMBER + ">");
		
		xmlSB.append(SimpleObj2XML.toXMLFragment(pi));

		return xmlSB.toString();
	}

	public String editConfirmation(ProductIdentification pi) throws Exception{
		ProductIdentification old = (ProductIdentification)this.dao.get(ProductIdentification.class.getName(), pi.getId());
		
		old.setConfirmedDate(pi.getConfirmedDate());
		old.setYarnSize1(pi.getYarnSize1());
		old.setYarnSize2(pi.getYarnSize2());
		old.setYarnSize3(pi.getYarnSize3());
		old.setYarnSize4(pi.getYarnSize4());
		old.setYarnSize5(pi.getYarnSize5());
		old.setYarnSize6(pi.getYarnSize6());
		old.setYarnSize7(pi.getYarnSize7());
		old.setYarnSize8(pi.getYarnSize8());
		old.setCoefficient(pi.getCoefficient());
		old.setIsConfirmed(pi.getIsConfirmed());
		
		this.dao.saveOrUpdate(old);
		super.editSysMbox(old, "02");
		return "{success: true}";
	}
	
//	public String editProductYarn(ProductYarn productYarn)throws Exception {
//		BiYarn yarn = (BiYarn)this.dao.get(BiYarn.class.getName(), productYarn.getBiYarn().getId());
//		Float stock = yarn.getStock();
//		if(productYarn.getId() != null){
//			ProductYarn old = (ProductYarn)this.dao.get(ProductYarn.class.getName(), productYarn.getId());
//			if(old.getBiYarn().getId() != productYarn.getBiYarn().getId()){
//				BiYarn biYarnOld = (BiYarn)this.dao.get(BiYarn.class.getName(), old.getBiYarn().getId());
//				Float stockOld = biYarnOld.getStock();
//				stockOld += old.getQuantity();
//				biYarnOld.setStock(stockOld);
//				this.dao.saveOrUpdate(biYarnOld);
//			}else{
//				stock += old.getQuantity();
//			}
//			this.dao.getSession().evict(old);
//		}
//		Float quantity = productYarn.getQuantity();
//		stock -= quantity;
//		yarn.setStock(stock);
//		this.dao.saveOrUpdate(yarn);
//		this.dao.saveOrUpdate(productYarn);
//		return "{success: true}";
//	}
	
//	public String getAllYarn(Integer piId) throws Exception{
//		String [] filters = new String[1];
//		filters[0] = COLUMN_PRODUCTIDENTIFICATION_ID + "=" + piId;
//		List<ProductYarn> objList = this.dao.getAll(ProductYarn.class.getName(), COLUMN_ID, "asc", filters);
//		StringBuffer xmlSB = new StringBuffer();
//		xmlSB.append(Constants.XML_DOC_DEFINE);
//		xmlSB.append(Constants.XML_ROOT_TAG[0]);
//		int offset = 0;
//		for (ProductYarn obj : objList) {
//			xmlSB.append(Constants.XML_ITEM_TAG[0]);
//			xmlSB.append(Constants.XML_INDEX_TAG[0]);
//			xmlSB.append(1);
//			xmlSB.append(Constants.XML_INDEX_TAG[1]);
//			
//			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
//			
//			xmlSB.append("<ctYarnType>");
//			xmlSB.append(Constants.XML_CDATA[0]);
//			xmlSB.append(this.ctService.getDescription(CtYarnType.class.getName(), obj.getBiYarn().getCtYarnType(), locale));
//			xmlSB.append(Constants.XML_CDATA[1]);
//			xmlSB.append("</ctYarnType>");
//			
//			xmlSB.append("<ctMaterialType>");
//			xmlSB.append(Constants.XML_CDATA[0]);
//			xmlSB.append(this.ctService.getDescription(CtMaterialType.class.getName(), obj.getBiYarn().getCtMaterialType(), locale));
//			xmlSB.append(Constants.XML_CDATA[1]);
//			xmlSB.append("</ctMaterialType>");
//			
//			xmlSB.append(SimpleObj2XML.toXMLFragment(obj.getBiYarn()));
//			xmlSB.append(Constants.XML_ITEM_TAG[1]);
//		}
//		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
//		xmlSB.append("1");
//		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);
//
//		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
//		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
//		xmlSB.append(Constants.XML_ROOT_TAG[1]);
//		return xmlSB.toString();
//	}
	
	public String getMaxReferenceIndex(String reference) throws Exception{
		List referenceIndexList = this.dao.getMaxReferenceIndex(reference);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(Constants.XML_CDATA[0]);
		if(referenceIndexList.size() == 0 || referenceIndexList.get(0) == null){
			xmlSB.append("0001");
		}else{
			Integer index = (Integer)referenceIndexList.get(0);
			xmlSB.append(StringUtils.to4Digital(++index));
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getMaxReferenceIndex(String ctCollarType, String ctSeasonNumber) throws Exception{
		List referenceIndexList = this.dao.getMaxReferenceIndex(ctCollarType, ctSeasonNumber);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(Constants.XML_CDATA[0]);
		if(referenceIndexList.size() == 0 || referenceIndexList.get(0) == null){
			xmlSB.append("0001");
		}else{
			Integer index = (Integer)referenceIndexList.get(0);
			xmlSB.append(StringUtils.to4Digital(++index));
		}
		xmlSB.append(Constants.XML_CDATA[1]);
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public ProductIdentification getObj(Integer id) throws Exception{
		return (ProductIdentification)this.dao.get(ProductIdentification.class.getName(), id);
	}
	
	public static Set<ProductMeasureSpecificationItem> cloneMeasureSpecificationItem(Set<BiMeasureSpecificationItem> biMeasureSpecificationItems){
		Set<ProductMeasureSpecificationItem> productMeasureSpecificationItems = new HashSet<ProductMeasureSpecificationItem>();
		for(BiMeasureSpecificationItem biMeasureSpecificationItem : biMeasureSpecificationItems){
			ProductMeasureSpecificationItem pmsi = new ProductMeasureSpecificationItem();
			pmsi.setCtMsiDescription(biMeasureSpecificationItem.getCtMsiDescription());
			pmsi.setDesignations(biMeasureSpecificationItem.getDesignations());
			pmsi.setNumber(biMeasureSpecificationItem.getNumber());
			pmsi.setTolerance(biMeasureSpecificationItem.getTolerance());
			
			pmsi.setSize1(biMeasureSpecificationItem.getSize1());
			pmsi.setIsModified1("02");
			pmsi.setSize2(biMeasureSpecificationItem.getSize2());
			pmsi.setIsModified2("02");
			pmsi.setSize3(biMeasureSpecificationItem.getSize3());
			pmsi.setIsModified3("02");
			pmsi.setSize4(biMeasureSpecificationItem.getSize4());
			pmsi.setIsModified4("02");
			pmsi.setSize5(biMeasureSpecificationItem.getSize5());
			pmsi.setIsModified5("02");
			pmsi.setSize6(biMeasureSpecificationItem.getSize6());
			pmsi.setIsModified6("02");
			pmsi.setSize7(biMeasureSpecificationItem.getSize7());
			pmsi.setIsModified7("02");
			pmsi.setSize8(biMeasureSpecificationItem.getSize8());
			pmsi.setIsModified8("02");

			productMeasureSpecificationItems.add(pmsi);
		}
		return productMeasureSpecificationItems;
	}
	
	public String getMSItemAll(int id) throws Exception {
		String [] filters = new String[1];
		filters[0] = "productIdentification.id = " + id;
		List objList = this.dao.getAll(ProductMeasureSpecificationItem.class.getName(), COLUMN_ITEM_NUMBER, "asc", filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		int offset = 0;
		for (Object obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			
			xmlSB.append("<ctMsiDescription>");
			xmlSB.append(Constants.XML_CDATA[0]);
			xmlSB.append(this.ctService.getDescription(CtMsiDescription.class.getName(), ((ProductMeasureSpecificationItem)obj).getCtMsiDescription(), locale));
			xmlSB.append(Constants.XML_CDATA[1]);
			xmlSB.append("</ctMsiDescription>");
			
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append("1");
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductMeasureSpecificationItem.class.getName(),filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getMSItem(int id) throws Exception {
		ProductMeasureSpecificationItem productMSItem = (ProductMeasureSpecificationItem) this.dao.get(
				ProductMeasureSpecificationItem.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append("<piId>");
		xmlSB.append(productMSItem.getProductIdentification().getId());
		xmlSB.append("</piId>");
		xmlSB.append(SimpleObj2XML.toXMLFragment(productMSItem));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public List<ProductYarn> getDiffYarn(ProductIdentification pi){
		List<ProductYarn> pyList = new ArrayList<ProductYarn>();
		Set<ProductYarn> pySet = pi.getProductYarns();
		for(ProductYarn py: pySet){
			if(!pyList.contains(py))
				pyList.add(py);
		}
		return pyList;
	}
	
	public int getMaxColorCountInOrder(ProductIdentification pi){
		int colorCount = 0;
		Set<ProductOrderItem> productOrderItems = pi.getProductOrderItems();
		for(ProductOrderItem poi : productOrderItems){
			if(poi.getProductOrderItemColors().size() > colorCount)
				colorCount = poi.getProductOrderItemColors().size();
		}
		
		return colorCount;
	}
	
	public List<Float> getColorPercentage(ProductIdentification pi){
		List<Float> percentageList = new ArrayList<Float>();
		Set<ProductOrderItem> productOrderItems = pi.getProductOrderItems();
		for(ProductOrderItem poi : productOrderItems){
			Set<ProductOrderItemColor> productOrderItemColors = poi.getProductOrderItemColors();
			for(ProductOrderItemColor poic : productOrderItemColors){
				percentageList.add(poic.getPercentage());
			}
			return percentageList;
		}
		return percentageList;
	}
	
	public String updateMS(Integer id, Integer msId) throws Exception {
		ProductIdentification pi = (ProductIdentification)this.dao.get(ProductIdentification.class.getName(), id);
		BiMeasureSpecification biMeasureSpecification = (BiMeasureSpecification)this.dao.get(BiMeasureSpecification.class.getName(), msId);
		pi.setBiMeasureSpecification(biMeasureSpecification);
		for (Iterator iterator = pi.getProductMeasureSpecificationItems().iterator(); iterator.hasNext();) {
			ProductMeasureSpecificationItem pmi = (ProductMeasureSpecificationItem) iterator.next();
			this.dao.delete(ProductMeasureSpecificationItem.class.getName(), pmi.getId());
		}
		Set<ProductMeasureSpecificationItem> productMeasureSpecificationItems = 
			cloneMeasureSpecificationItem(biMeasureSpecification.getBiMeasureSpecificationItems());
		
		pi.setProductMeasureSpecificationItems(productMeasureSpecificationItems);
		this.dao.saveOrUpdate(pi);
		if(productMeasureSpecificationItems != null){
			for(ProductMeasureSpecificationItem pmsi : productMeasureSpecificationItems){
				pmsi.setProductIdentification(pi);
				this.dao.saveOrUpdate(pmsi);
			}
		}
		return "{success: true}";
	}
	
	public String getVariant(Integer id) throws Exception{
		List variantList = this.dao.getDifferentVariant(id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		
		for(Object obj : variantList){
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append("<code>");
			xmlSB.append(obj);
			xmlSB.append("</code>");
			xmlSB.append("<description>");
			xmlSB.append(obj);
			xmlSB.append("</description>");
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append("1");
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(variantList.size());
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		
		return xmlSB.toString();
	}
	
	public String goBack(Integer id) throws Exception{
		ProductIdentification pi = (ProductIdentification)this.dao.get(ProductIdentification.class.getName(), id);
		pi.setIsConfirmed("02");
		this.dao.saveOrUpdate(pi);
		return "success";
	}
	
	private void cloneProductIdentification(ProductIdentification pi) throws Exception{
		Object similar = this.dao.get(ProductIdentification.class.getName(), pi.getSimilarId());
		if(similar == null){
			return;
		}
		ProductIdentification similaryPI = (ProductIdentification)similar;
		pi.setWeight(similaryPI.getWeight());
		pi.setCoefficient(similaryPI.getCoefficient());
		Set<ProductDZColor> pDZColors = similaryPI.getProductDZColors();
		for (ProductDZColor productDZColor : pDZColors) {
			ProductDZColor clonePDZColor = (ProductDZColor)productDZColor.cloneOne();
			clonePDZColor.setProductIdentification(pi);
			this.dao.saveOrUpdate(clonePDZColor);
		}
		/*
		pi.setBiMeasureSpecification(similaryPI.getBiMeasureSpecification());
		this.dao.saveOrUpdate(pi);
		
		Set<ProductMeasureSpecificationItem> pMSItems = similaryPI.getProductMeasureSpecificationItems();
		for (ProductMeasureSpecificationItem productMeasureSpecificationItem : pMSItems) {
			ProductMeasureSpecificationItem clonePMSItem = productMeasureSpecificationItem.cloneOne();
			clonePMSItem.setProductIdentification(pi);
			this.dao.saveOrUpdate(clonePMSItem);
		}
		
		Set<ProductLaunching> plSets = similaryPI.getProductLaunchings();
		for (ProductLaunching productLaunching : plSets) {
			ProductLaunching clonePl = productLaunching.cloneOne();
			clonePl.setProductIdentification(pi);
			this.dao.saveOrUpdate(clonePl);
		}*/
		
		Set<ProductAccessoryButton> pABs = similaryPI.getProductAccessoryButtons();
		for (ProductAccessoryButton productAccessoryButton : pABs) {
			ProductAccessoryButton clonePAB = productAccessoryButton.cloneOne();
			clonePAB.setProductIdentification(pi);
			//if(productAccessoryButton.getBiAccessoryButton().getBiFranceCustomer().equals(pi.getBiFranceCustomer())){
				this.dao.saveOrUpdate(clonePAB);
			//}
		}
		
		Set<ProductAccessoryZip> pZips = similaryPI.getProductAccessoryZips();
		for (ProductAccessoryZip productAccessoryZip : pZips) {
			ProductAccessoryZip clonePAZ = productAccessoryZip.cloneOne();
			clonePAZ.setProductIdentification(pi);
			//if(productAccessoryZip.getBiAccessoryZip().getBiFranceCustomer().equals(pi.getBiFranceCustomer())){
				this.dao.saveOrUpdate(clonePAZ);
			//}
		}
		
		Set<ProductAccessoryOthers> pOther = similaryPI.getProductAccessoryOtherses();
		for (ProductAccessoryOthers productAccessoryOthers : pOther) {
			ProductAccessoryOthers clonePAO = productAccessoryOthers.cloneOne();
			clonePAO.setProductIdentification(pi);
			//if(productAccessoryOthers.getBiAccessoryOthers().getBiFranceCustomer().equals(pi.getBiFranceCustomer())){
				this.dao.saveOrUpdate(clonePAO);
			//}
		}
		
		Set<ProductAccessoryHang> pHang = similaryPI.getProductAccessoryHangs();
		for(ProductAccessoryHang hang : pHang){
			ProductAccessoryHang cloneHang = hang.cloneOne();
			cloneHang.setProductIdentification(pi);
			//if(hang.getBiAccessoryOthers1().getBiFranceCustomer().equals(pi.getBiFranceCustomer())){
				this.dao.saveOrUpdate(cloneHang);
			//}
		}
	}
	
	public static void main(String[] args) throws Exception{
		ProductIdentificationServiceImpl piService = new ProductIdentificationServiceImpl();
		System.out.println(piService.getVariant(6));
	}
}